#include "dlg_setting.h"
#include "ui_dlg_setting.h"

dlg_Setting::dlg_Setting(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::dlg_Setting)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
}

void dlg_Setting::setSetting()
{
    initWidget();
}

dlg_Setting::~dlg_Setting()
{
    delete ui;
}

void dlg_Setting::initWidget()
{
    this->loadQSS();
    ui->lineEdit_pythonPath->setText(sys_Data::getInstance()->getCondaEnvir());
    ui->lineEdit_analysisPath->setText(sys_Data::getInstance()->getAnalysisAlgPath());
    ui->spinBox_openFIleRecord->setValue(sys_Data::getInstance()->getMaxOpenFileHistoryRecord());
    ui->comboBox_theme->clear();
    ui->comboBox_theme->addItems(sys_Data::Theme);
    ui->comboBox_theme->setCurrentIndex(sys_Data::Theme.indexOf(sys_Data::getInstance()->getThemePath()));
    this->exec();
}

void dlg_Setting::loadQSS()
{
    QString style = sys_Data::getInstance()->getThemeQss();
    this->setStyleSheet(style);//设置主题样式
}

void dlg_Setting::setPath(QLineEdit *_LE)
{
    QFileDialog fileDialog;
    //不使用标准文件对话框
    fileDialog.setOption(QFileDialog::DontUseNativeDialog);
    //去掉文件对话框边框
    fileDialog.setWindowFlag(Qt::FramelessWindowHint);
    fileDialog.setFileMode(QFileDialog::Directory);
    fileDialog.setStyleSheet(sys_Data::getInstance()->getThemeQss());
    foreach(QPushButton* btn, fileDialog.findChildren<QPushButton*>()) {
        btn->setFixedSize(80, 20);//设置文件对话框按钮大小
    }
    if(fileDialog.exec() == QFileDialog::AcceptSave) {
        QStringList path = fileDialog.selectedFiles();
        _LE->setText(path.first());
    }
}

void dlg_Setting::on_pushButton_choosePythonPath_clicked()
{
    setPath(ui->lineEdit_pythonPath);
}

void dlg_Setting::on_pushButton_chooseAnalysisPath_clicked()
{
    setPath(ui->lineEdit_analysisPath);
}

void dlg_Setting::on_pushButton_use_clicked()
{
    sys_Data *sys = sys_Data::getInstance();
    sys->setMaxOpenFileHistoryRecord(ui->spinBox_openFIleRecord->value());
    sys->setThemePath(ui->comboBox_theme->currentText());
    sys->setCondaEnvir(ui->lineEdit_pythonPath->text());
    sys->setAnalysisAlgPath(ui->lineEdit_analysisPath->text());
    this->close();
}

void dlg_Setting::on_pushButton_cancel_clicked()
{
    this->close();
}
